"""
# Gorb 
# Copyright Google, 2007-2008
# Author: Jason Huggins (hugs@google.com)
"""

import serial
  
# IMPORTANT!!!  
# 
# You'll need to change SERIAL_PORT_ID to match your Arduino's serial port.
# To find the right port, look in the Arduino IDE while the 
# Arduino is plugged in:
#     Tools menu -> Serial Port --> <the-right-one-will-be-here>
#
# For for more information on serial port connections, please refer
# to the pySerial website (http://pyserial.sourceforge.net/)
# 
# TODO (hugs): make this automagically set during setup/install.
#SERIAL_PORT_ID = "/dev/tty.usbserial-A4001ubI"  
SERIAL_PORT_ID = ""

if SERIAL_PORT_ID == "":
  raise "\nERROR: Serial Port ID is missing" + \
        "\nYou need to set the Serial Port ID of your Arduino!" + \
        "\nPlease edit 'SERIAL_PORT_ID' in gorb/orb.py with this information\n"

class Orb:
  def __init__(self):
    self.serial = serial.Serial(SERIAL_PORT_ID,9600,timeout=1)

  def send_command(self, command, red=0, green=0, blue=0):
    self.serial.write(command)
    for color in [red, green, blue]:
      colorAsString = chr(color) 
      self.serial.write( colorAsString )
   
  def set_colors(self, red=0, green=0, blue=0):
    return self.send_command("0", red, green, blue)
    
  # TODO (hugs): Once proper fading is done in "set_colors", delete this method.
  def fade_in(self, red=0, green=0, blue=0):
    return self.send_command("1", red, green, blue)
  
  # TODO (hugs): Once proper fading is done in "set_colors", delete this method.
  def fade_out(self, red=0, green=0, blue=0):
    return self.send_command("2", red, green, blue)
  
  # All the colors of the rainbow 
  def red(self):
    self.set_colors(255,0,0)
  
  def orange(self):
    # Orange is 75% red, 25% green
    self.set_colors(255,85,0)
  
  def yellow(self):
    # Yellow is 50% red, 50% green
    self.set_colors(255,255,0)
  
  def green(self):
    self.set_colors(0,255,0)
  
  def blue(self):
    self.set_colors(0,0,255)
  
  def purple(self):
    # Purple is 50% red, 50% blue
    self.set_colors(255,0,255)
  
  def white(self):
    self.set_colors(255,255,255)
  
  def lights_out(self):
    return self.send_command("0")

__test__ = {
  "gorb" :
  r"""
  # To be implemented:
  
  # Various settings
  o.pulse = True      # Whether the color will be pulsed bright/dark forever
  o.transition = 1    # How fast to transition between colors (0=slow, 10=fast)
  
  # Multiple ways of sending color values
  o.rgb(127,255,0)   # straight r,g,b
  o.rgb(r=127,g=100,b=200) # as named parameters (default value is 0)
  o.rgb("#FF00DD") # Sending classic "web/html" encodings

  # People looking at the orb, will be able to see the message as bright/dark pulses.
  o.morse("sos")    
  o.morse("... --- ...")  # really old school 

  o.ping()  # check to see if the orb is alive
  o.info()  # ask the orb for any information it has about itself

  o.cycle() # cycle between all colors
  o.cycle(["#00FF00","red"])  # cycle between 2 colors, supportin both hex and string
  
  o.roam() # cycle randomly through all colors
  
  # I'll need this if I support hex (#FF00FF) encodings:
  def toHex(str): return int(str,16)  
  
  # This is what I'd like to support sending over the wire,
  # instead of the current 4-byte integer protocol:
  (rgb 255 100 0)
  (pulse on)
  (pulse off)
  (speed 10)
  (morse "sos")
  (ping)
  (info)
  (cycle all)
  (cycle {255 0 0} {0 255 0} )
  """
}


if __name__ == '__main__':
      print """
  Gorb
  Copyright Google, 2007-2008
  Author: Jason Huggins (hugs@google.com)
      
  Usage: 
    When used as library:
      >>> from gorb.orb import Orb
      >>> o = Orb()
      >>> o.green()
      """
    
